Skip to main content

Leetcode 1318

· One min read
Junhe Chen

bit manipulation

class Solution {
public int minFlips(int a, int b, int c) {
int res = 0;
while(a != 0 || b != 0 || c!= 0){
// there is more we going to exampe
if((c & 1) == 1){
// we have c curr bit at one
if ((a & 1) == 0 && (b & 1) == 0) {
// we need to flip this c
res++;
}
}else{
res += (a & 1) + (b & 1); // flip a or b to 0 if needed
}
//exam next bit

a >>= 1;
b >>= 1;
c >>= 1;

}
return res;
}
}